local skel = fk.CreateSkill {
  name = "lb__shiyi",
  tags = {Skill.Limited},
}

Fk:loadTranslationTable {
  ["lb__shiyi"] = "拾忆",
  --[":lb__shiyi"] = "每名角色限一次，出牌阶段，你可以记录一名角色当前牌数。其首次因每种方式失去牌后，若其牌数不等于记录值，将之调整为记录值。",
  --9.16
  [":lb__shiyi"] = "<b>限定技</b>，出牌阶段，你可以分别记录你与一名其他角色的当前牌数，你或其首次因每种方式失去牌后，若其牌数不等于记录值，将之调整为记录值。",

  --["#lb__shiyi"] = "拾忆：你可以记录一名角色当前牌数，其首次因每种方式失去牌后调整牌数至当前牌数",
  ["#lb__shiyi"] = "拾忆：你可以记录你与一名其它角色的当前牌数，你或其首次因每种方式失去牌后调整牌数至当前牌数",
  ["#lb__shiyi_dis"] = "拾忆：请弃置 %arg 张牌",

  ["@lb__shiyi"] = "拾忆",
  ["@[string]lb__shiyi"] = "拾忆方式",

  ["$lb__shiyi1"] = "要陪我去拍照吗？",
  ["$lb__shiyi2"] = "来尝尝本姑娘的厉害！",
  ["$lb__shiyi3"] = "我的过去，正在一点点变多呢。",
  ["$lb__shiyi4"] = "今天的我比昨天更棒了。",
}

skel:addEffect("active", {
  audio_index = {1, 2},
  prompt = "#lb__shiyi",
  can_use = function (self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    local list = Fk:currentRoom():getBanner(skel.name .. player.id) or {}
    return #selected == 0 and list[tostring(to_select.id)] == nil and to_select ~= player
  end,
  feasible = function(self, player, selected, selected_cards, card)
    return #selected == 1
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    --local num = #target:getCardIds("he")
    local list = room:getBanner(skel.name .. player.id) or {}
    for _, to in ipairs({player, target}) do
      local num = #to:getCardIds("he")
      list[tostring(to.id)] = { num, {} }
      room:setPlayerMark(to, "@lb__shiyi", tostring(num))
    end
    --list[tostring(target.id)] = { num, {} }
    room:setBanner(skel.name .. player.id, list)
    --room:setPlayerMark(target, "@lb__shiyi", tostring(num))
  end,
})

skel:addEffect(fk.AfterCardsMove, {
  anim_type = "control",
  is_delay_effect = true,
  audio_index = {3, 4},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(skel.name, true) then
      local room = player.room
      local list = room:getBanner(skel.name .. player.id) or {}
      for _, move in ipairs(data) do
        if move.from and list[tostring(move.from.id)] and not move.from.dead 
            and table.find(move.moveInfo, function(info)
              return info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip
            end) then
          local num, reason_list = table.unpack(list[tostring(move.from.id)])
          if reason_list and table.insertIfNeed(reason_list, move.moveReason) then
            list[tostring(move.from.id)][2] = reason_list
            room:setBanner(skel.name .. player.id, list)
            room:setPlayerMark(move.from, "@[string]lb__shiyi", table.map(list[tostring(move.from.id)][2], function (reason)
              return Util.moveReasonMapper(reason)
            end))
            --event:setCostData(self,{reason = reason, tos = {move.from}})
            event:setCostData(self, { num = num, tos = { move.from } })
            return player:hasSkill(skel.name) and #move.from:getCardIds("he") ~= num
          end
        end
      end
    end 
  end,
  on_use = function(self, event, target, player, data)
    --[[  --看测试结果，有必要再加上宝宝标记
      local reason_table = {
        [fk.ReasonJustMove] = "移动",
        [fk.ReasonDiscard] = "弃置",
        [fk.ReasonGive] = "交出",
        [fk.ReasonPut] = "置入",
        [fk.ReasonPutIntoDiscardPile] = "置入弃牌堆",
        [fk.ReasonPrey] = "获得",
        [fk.ReasonExchange] = "交换",
        [fk.ReasonUse] = "使用",
        [fk.ReasonResponse] = "打出",
        [fk.ReasonRecast] = "重铸",
        [101] = "拼点",
      }
      local reason = event:getCostData(self).reason
      if not reason_table[reason] then return end
      room:addTableMark(player,"sj3__zhanghua",reason)
      local mark = ""
      for _, v in ipairs(player:getTableMark("sj3__zhanghua")) do
        mark = mark..reason_table[v]
      end
      room:setPlayerMark(player,"@sj3__zhanghua",mark)
    --]]
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local num = event:getCostData(self).num - #to:getCardIds("he")
    if num > 0 then
      to:drawCards(num, skel.name)
    elseif num < 0 then
      num = math.abs(num)
      room:askToDiscard(to, {
        min_num = num,
        max_num = num,
        skill_name = skel.name,
        cancelable = false,
        include_equip = true,
        prompt = "#lb__shiyi_dis:::"..num,
      })
    end
  end,
})

skel:addLoseEffect(function (self, player, is_death)
  local room = player.room
  room:setBanner(skel.name .. player.id, nil)
  for _, to in ipairs(room.players) do
    room:setPlayerMark(to, "@lb__shiyi", 0)
    room:setPlayerMark(to, "@[string]lb__shiyi", 0)
  end
end)

return skel